/*
 * Copyright 2002-2016 the Beijing Brain Technology co.,LTD http://www.bris.cn/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bris.niosocket.mapping;

import com.bris.niosocket.message.impl.RequestMessage;
import com.bris.niosocket.message.impl.ResponseMessage;

/**
 * 转发SOCKET请求</br>
 * 2016年2月23日</br>
 * @author xunaidong
 * @since 1.0.0
 */
public class SocketDispather {
	
	private HandlerMapping handlerMapping;
	
	/**
	 * @param handlerMapping the handlerMapping to set
	 */
	public void setHandlerMapping(HandlerMapping handlerMapping) {
		this.handlerMapping = handlerMapping;
	}

	public void doDispath(RequestMessage request,ResponseMessage response) throws Exception{
		
		HandlerChain chain = handlerMapping.getHandler(request);
		
		Object handler = chain.getHandler();
		
		if(handler instanceof HandlerMethod){
			/*处理器调用之前*/
			chain.applyPreHandle(request, response);
			((HandlerMethod)handler).getMethod().invoke(((HandlerMethod)handler).getBean(), new Object[]{request,response});
			/*处理器调用之后*/
			chain.applyPostHandle(request, response);
		}else{
			throw new Exception("暂未支持的Handler"+handler);
		}
		
		
	}
	
}
